package sourceagile.server.localFileRepository;

import java.io.File;

import org.codehaus.jettison.json.JSONArray;

import sourceagile.server.utilities.ConvertFileToJson;

public class ListLocalFiles {

	public static JSONArray listEntries(String projectName) {

		JSONArray entries = new JSONArray();

		String projectPath = System.getProperty("java.io.tmpdir") + "/"
				+ projectName;

		File path = new File(projectPath);

		entries = listSubDirectory(path, entries, projectPath);

		return entries;
	}

	public static JSONArray listSubDirectory(File dir, JSONArray entries,
			String projectPath) {

		if (dir.isDirectory()) {

			String[] children = dir.list();

			for (int i = 0; i < children.length; i++) {

				listSubDirectory(new File(dir, children[i]), entries,
						projectPath);
			}

		} else {

			entries.put(ConvertFileToJson
					.convertFileFromLocal(dir, projectPath));
		}

		return entries;
	}

}
